package com.bayern.main;

import java.io.InputStream;
import java.io.StringWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONObject;

import com.bayern.ip.segment.ChinaEducationNetwork;
import com.bayern.ip.segment.ChinaMobile;
import com.bayern.ip.segment.ChinaOther;
import com.bayern.ip.segment.ChinaRailcom;
import com.bayern.ip.segment.ChinaTelecom;
import com.bayern.ip.segment.ChinaUnicom;
import com.bayern.util.IPUtils;
import com.bayern.vo.VoIpSegment;

public class GetIPSegment {
	//模拟UA
    public static String UA = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.28 (KHTML, like Gecko) Chrome/26.0.1397.2 Safari/537.28";
    
    
    public void getIps() {
        try {
        	long sum = 0;
            DefaultHttpClient hc = new DefaultHttpClient();
            InputStream is;
            
            List<String> ipSegmentList = new ArrayList<String>();
            ipSegmentList.addAll(Arrays.asList(ChinaEducationNetwork.educationNetworkSegment));
            ipSegmentList.addAll(Arrays.asList(ChinaMobile.mobileSegment));
            ipSegmentList.addAll(Arrays.asList(ChinaOther.otherSegment));
            ipSegmentList.addAll(Arrays.asList(ChinaRailcom.railcomSegment));
            ipSegmentList.addAll(Arrays.asList(ChinaTelecom.telecomSegment));
            ipSegmentList.addAll(Arrays.asList(ChinaUnicom.unicomSegment));
            
            for(Iterator<String> iter = ipSegmentList.iterator(); iter.hasNext();) {
            	String ipStr = iter.next();
            	String[] strs = ipStr.split("\\/");
            	String ip = strs[0];
            	int mask = Integer.valueOf(strs[1]);

				sum++;
				//System.out.print(i + "." + ii + "." + iii + "." + iiii + ";");
				
				HttpGet http_get = new HttpGet(
							"http://ip.taobao.com/service/getIpInfo.php?ip="+ ip);
					http_get.setHeader("User-Agent", UA);
					VoIpSegment vo = new VoIpSegment();
					String ipEnd = IPUtils.getIpEnd(ip, mask);
					vo.setIpStart(ip);
					vo.setIpEnd(ipEnd);
					vo.setIpStartNumber(IPUtils.convertIpToLong(ip));
					vo.setIpEndNumber(IPUtils.convertIpToLong(ipEnd));
					
					int count = 0;
					do {
						try {
							count++;
							HttpEntity e = hc.execute(http_get)
									.getEntity();
							is = e.getContent();
							StringWriter sw = new StringWriter();
							IOUtils.copy(is, sw);
							is.close();
							String res = sw.toString();
							System.out.println(res);
							JSONObject resp = new JSONObject(
									res);

							if (resp.has("data")) {
								JSONObject data = resp
										.getJSONObject("data");
								if (data.has("ip")) {
									String rip = data
											.getString("ip");
								}
								if (data.has("country")) {
									vo.setCountry(data
											.getString("country"));
								}
								if (data.has("country_id")) {
									vo.setCountryId(data
											.getString("country_id"));
								}
								if (data.has("area")) {
									vo.setArea(data
											.getString("area"));
								}
								if (data.has("area_id")) {
									vo.setAreaId(data
											.getString("area_id"));
								}
								if (data.has("region")) {
									vo.setRegion(data
											.getString("region"));
								}
								if (data.has("region_id")) {
									vo.setRegionId(data
											.getString("region_id"));
								}
								if (data.has("city")) {
									String rcity = data
											.getString("city");
									vo.setCity(rcity);
								}
								if (data.has("city_id")) {
									vo.setCityId(data
											.getString("city_id"));
								}
								if (data.has("county")) {
									vo.setCounty(data
											.getString("county"));
								}
								if (data.has("county_id")) {
									vo.setCountyId(data
											.getString("county_id"));
								}
								if (data.has("isp_id")) {
									vo.setIspId(data
											.getString("isp_id"));
								}
								if (data.has("isp")) {
									vo.setIsp(data
											.getString("isp"));
								}
							}

						} catch (Exception e) {
							e.printStackTrace();
						}
						// 因为淘宝对每秒的访问次数有限制，这里尝试十次，如果还拿不到城市，就跳过。
					} while (StringUtils.isBlank(vo.getIsp())
							&& count < 10);
					System.out.println("IP地址：" + ip
							+ "  ---- 城市   -----"
							+ vo.getCity());
				 
            }
        } catch (Exception e) {
            e.printStackTrace();
        } 
    }
    
    public static void main(String[] args) {
    	GetIPSegment getIp = new GetIPSegment();
    	getIp.getIps();
    	
    }
}
